package com.nick.service.ipml;



import com.nick.dao.ItemsMapper;
import com.nick.pojo.Items;
import com.nick.pojo.ItemsExample;
import com.nick.pojo.Page;
import com.nick.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by �� on 2017/6/13.
 */
@Service("itemService")
public class ItemServiceIpml implements ItemService{
    @Autowired
    private ItemsMapper itemsMapper;

//    根据ID查询商品
    public List<Items> getItemById(int itmeId) {
        return itemsMapper.selectByPrimaryKey(itmeId);
    }

//    查询所有商品
    public List<Items> findAllItems() {
        return   itemsMapper.selectByExample(null);
    }

//    用户密码验证
    public Items getItemByNameAndPass(Integer id, String Name) {
        ItemsExample example = new ItemsExample();
        ItemsExample.Criteria criteria= example.createCriteria();
        criteria.andIdEqualTo(id);
        criteria.andNameEqualTo(Name);
         List<Items> list =   itemsMapper.selectByExample(example);
//        获取一条数据
        return list.get(0);
    }

//    增加商品
    public Integer addItem(Items items) {
        return itemsMapper.insert(items);
    }

//    更新商品
    public Integer updateItem(Items items) {
        return itemsMapper.updateByPrimaryKey(items);
    }

//    删除商品
    public Integer deleteItem(Integer itemid) {
        return itemsMapper.deleteByPrimaryKey(itemid);
    }

    public List<Items> selectItemsByPage(Integer start, Integer end) {
        return null;
    }

    public void showItemsByPage(HttpServletRequest request, Model model) {
        String pageNow=request.getParameter("pageNow");
        Page page=null;
        List<Items> list=new ArrayList<Items>();
        int totalCount=(int)itemsMapper.getItemsCount();
        if (pageNow!=null){
            page=new Page(Integer.parseInt(pageNow),totalCount);
            list=itemsMapper.selectItemsByPage(page.getStartPos(),page.getPageSize());
        }else{
            page=new Page(1,totalCount);
            list=itemsMapper.selectItemsByPage(page.getStartPos(),page.getPageSize());
        }
        model.addAttribute("items",list);
        model.addAttribute("page",page);
    }

    public long getItemsCount() {
        return itemsMapper.getItemsCount();
    }

    public List<Items> findByName(String name) {
        ItemsExample example = new ItemsExample();
        ItemsExample.Criteria criteria= example.createCriteria() ;
        criteria.andNameLike("%"+name+"%");
        return itemsMapper.selectByExample(example);
    }

}
